using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

namespace TimeWar
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class TeamSelection : TimeWar.Screen
    {
        private List<Companion> companions = null;
        private Cultures culture;
        private Game gameObj;
        private GameCursor cursor;
        private SpriteBatch spriteBatch = null;

        public TeamSelection(Game game, Cultures selectedCulture)
        {
            // TODO: Construct any child components here
            companions = new List<Companion>();
            culture = selectedCulture;
            gameObj = game;

            Initialize();
        }

        public Cultures Culture
        {
            get
            {
                return culture;
            }
        }
        

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public void Initialize()
        {
            // TODO: Add your initialization code here
            if (culture == Cultures.TimeLords)
            {
                Companion friend = new Companion("Captain Jack Harkness", @"Textures\\CaptainJackIcon", @"Textures\\CaptainJackFull");
                companions.Add(friend);
                friend = new Companion("Optimus Prime", @"Textures\\OptimusPrimeIcon", @"Textures\\OptimusPrimeFull");
                companions.Add(friend);
                friend = new Companion("Chuck", @"Textures\\ChuckIcon", @"Textures\\ChuckFull");
                companions.Add(friend);
                friend = new Companion("Apollo", @"Textures\\LeeAdamaIcon", @"Textures\\LeeAdamaFull");
                companions.Add(friend);



            }

            if (gameObj != null)
            {
                IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)gameObj.Services.GetService(typeof(IGraphicsDeviceService));
                spriteBatch = new SpriteBatch(graphicsService.GraphicsDevice);
            }

            cursor = new GameCursor(spriteBatch);
            cursor.Initialize();

        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override GameState Update(TimeSpan time, TimeSpan elapsedTime)
        {
            //XInputHelper.GamePads.Update(gameObj, new KeyboardState());
            XInputHelper.GamePads[PlayerIndex.One].Update(gameObj, new KeyboardState());

            if (XInputHelper.GamePads[PlayerIndex.One].APressed)
            {
                return GameState.PlayDemo;
            }

            return GameState.TeamSelection;


        }

        public override void Render()
        {
            Rectangle destRect = new Rectangle(0, 0, 200, 200);
            Vector2 originVector = new Vector2(0, 0);

            //Sprites will always be at the back.
            gameObj.GraphicsDevice.Clear(ClearOptions.Target, Color.Black, 1.0f, 0);
   

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.BackToFront, SaveStateMode.None);
            //spriteBatch.Begin(SpriteBlendMode.None, SpriteSortMode.Immediate, SaveStateMode.None);

            cursor.setSpriteBatch(spriteBatch);
            //SpriteFont testFont = TimeWarGame.ContentManager.Load<SpriteFont>("Content\\Fonts\\CourierNew");
            //SpriteBatch.DrawString(testFont, "Hello world", new Vector2(0, 0), Color.Red);

            foreach (Companion obj in companions)
            {
                spriteBatch.Draw(obj.Icon, destRect, obj.OriginalIcon, Color.White, 0, originVector, SpriteEffects.None, 1);
                //obj.Position = originVector;
                //SpriteBatch.Draw(obj.Icon, obj.Position, null, Color.White, 0, new Vector2(0, 0), 0, SpriteEffects.None, 1);
                //SpriteBatch.Draw(obj.Icon, obj.Position, Color.White);
                destRect.X = destRect.X + destRect.Width;
                //originVector.X = originVector.X + obj.Icon.Width;
            }

            cursor.Render();

            //const int FixedDrawingWidth = 1280;
            //const int FixedDrawingHeight = 720;

            //PresentationParameters pp = gameObj.GraphicsDevice.PresentationParameters;

            //RenderTarget2D drawBuffer = new RenderTarget2D(gameObj.GraphicsDevice,
            //                                FixedDrawingWidth, FixedDrawingHeight, 1,
            //                                SurfaceFormat.Color,
            //                                pp.MultiSampleType, pp.MultiSampleQuality);
            //gameObj.GraphicsDevice.DepthStencilBuffer = null;
            //gameObj.GraphicsDevice.SetRenderTarget(0, null);

            //PresentationParameters presentation = gameObj.GraphicsDevice.PresentationParameters;

            //float outputAspect = (float)presentation.BackBufferWidth / (float)presentation.BackBufferHeight;
            //float preferredAspect = (float)FixedDrawingWidth / (float)FixedDrawingHeight;

            //int presentWidth = (int)((presentation.BackBufferHeight * preferredAspect) + 0.5f);
            //int barWidth = (presentation.BackBufferWidth - presentWidth) / 2;

            //Rectangle dst = new Rectangle(barWidth, 0, presentWidth, presentation.BackBufferHeight);


            //spriteBatch.Draw(drawBuffer.GetTexture(), dst, Color.White);

            spriteBatch.End();
            

        }

        public override void Shutdown()
        {
            if (spriteBatch != null)
            {
                spriteBatch.Dispose();
                spriteBatch = null;
            }
        }

    }
}